<%= form_for(@sale) do |f| %>
    <% if @sale.errors.any? %>
        <div id="error_explanation" class="red">
          <ul>
            <% @sale.errors.full_messages.each do |message| %>
                <li><%= message %></li>
            <% end %>
          </ul>
        </div>
    <% end %>

    <div class="field">
      <%= f.label :sold_at %><br>
      <%= text_field_tag 'sale[sold_at]', @sale.sold_at.strftime('%F'), readonly: true %>
    </div>
    <div class="field">
      <%= f.label :user_id %><br>
      <%= f.text_field :user_id, style: 'width: 200px;', required: true %>
    </div>
    <div class="field">
      <%= f.label :customer_id %><br>
      <%= f.text_field :customer_id, style: 'width: 200px;', required: true %>
      <%= link_to '添加客户', new_customer_path if can? :create, Customer %>
    </div>
    <div class="field">
      <%= f.label :product_id %><br>
      <%= f.text_field :product_id, style: 'width: 200px;', required: true %>
    </div>
    <div class="field">
      <%= f.label :product_number %><br>
      <%= f.number_field :product_number, required: true %>
    </div>

    <% if false %>
    <div class="field">
      <%= f.label :sales_reward %><br>
      <%= f.text_field :sales_reward, rquired: true %>
    </div>
    <% end %>

    <% if action_name.in? ['edit', 'update'] %>
        <div class="field">
          <%= f.label :change_explain %><br>
          <%= f.text_area :change_explain %>
        </div>
    <% end %>

    <div class="actions m-t-10">
      <%= link_to '取消', sales_path, class: 'btn btn-default' %>
      <%= f.submit '保存', class: 'btn btn-primary' %>
    </div>
<% end %>

<% content_for :javascript do %>
    <script>
        $(function () {
            $("form").validate({
                rules: {
                    "sale[sold_at]": "required",
                    "sale[user_id]": "required"
                },
                submitHandler: function(form) {
                    if(!$('#sale_user_id').select2('val')){
                        alert('员工必须填写');
                        return;
                    }

                    if(!$('#sale_customer_id').select2('val')){
                        alert('客户必须填写');
                        return;
                    }

                    if(!$('#sale_product_id').select2('val')){
                        alert('产品必须填写');
                        return;
                    }

                    form.submit();
                }
            });


            $('#sale_sold_at').datepicker({
                format: "yyyy-mm-dd",
                language: "zh-CN",
                weekStart: 1,
                autoclose: true,
                todayHighlight: true
            });

            $('#sale_user_id').select2({
                language: "zh-CN",
                placeholder: "姓名或者手机号",
                allowClear: true,
                minimumInputLength: 1,
                ajax: {
                    url: "/staffs.json",
                    dataType: 'json',
                    delay: 250,
                    data: function (term) {
                        return {
                            "q[name_or_phone_cont]": term
                        };
                    },
                    results: function (data, page) {
                        var result = [];
                        for(var i = 0; i < data.length; i++) {
                            var d = data[i];
                            d['text'] = '#' + d.id + ' ' + d['name'] + ' ' + d['phone'];
                            result.push(d);
                        }
                        return {
                            results: result
                        };
                    },
                    cache: false
                },
                initSelection: function(element, callback) {
                    var id = $(element).val();
                    if (id !== "") {
                        $.ajax("/staffs/" + id + ".json", {
                            dataType: "json"
                        }).done(function(data) {
                            var d = data;
                            d['text'] = '#' + d.id + ' ' + d['name'] + ' ' + d['phone'];
                            callback(data);
                        });
                    }
                }
            });

            $('#sale_product_id').select2({
                language: "zh-CN",
                placeholder: "产品名称",
                allowClear: true,
                minimumInputLength: 1,
                ajax: {
                    url: "/products.json",
                    dataType: 'json',
                    delay: 250,
                    data: function (term) {
                        return {
                            "q[name_cont]": term
                        };
                    },
                    results: function (data, page) {
                        var result = [];
                        for(var i = 0; i < data.length; i++) {
                            var d = data[i];
                            d['text'] = '#' + d.id + ' ' + d['name'];
                            result.push(d);
                        }
                        return {
                            results: result
                        };
                    },
                    cache: false
                },
                initSelection: function(element, callback) {
                    var id = $(element).val();
                    if (id !== "") {
                        $.ajax("/products/" + id + ".json", {
                            dataType: "json"
                        }).done(function(data) {
                            var d = data;
                            d['text'] = '#' + d.id + ' ' + d['name'];
                            callback(data);
                        });
                    }
                }
            });

            $('#sale_customer_id').select2({
                language: "zh-CN",
                allowClear: true,
                minimumInputLength: 1,
                placeholder: "客户姓名",
                ajax: {
                    url: "/customers.json",
                    dataType: 'json',
                    delay: 250,
                    data: function (term) {
                        return {
                            "q[name_cont]": term
                        };
                    },
                    results: function (data, page) {
                        var result = [];
                        for(var i = 0; i < data.length; i++) {
                            var d = data[i];
                            d['text'] = '#' + d.id + ' ' + d['name'];
                            result.push(d);
                        }
                        return {
                            results: result
                        };
                    },
                    cache: false
                },
                initSelection: function(element, callback) {
                    var id = $(element).val();
                    if (id !== "") {
                        $.ajax("/customers/" + id + ".json", {
                            dataType: "json"
                        }).done(function(data) {
                            var d = data;
                            d['text'] = '#' + d.id + ' ' + d['name'];
                            callback(data);
                        });
                    }
                }
            });
        });
    </script>
<% end %>
